<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Index extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
        $this->load->model('User_model');
        $this->load->model('Usernum_model');
        $this->load->model('cert_model');
    }
	

	public function index($userid)
	{
		//浏览
		$sysVisittype = _get_config('visittype');
		$this->load->service('user_service');
		$this->user_service->visit($userid, $this->loginID, $sysVisittype['home']);
		//-浏览

		$oUser = $this->user_service->get_user_homeinfo($userid, $this->loginID);
        $insid = $oUser['insid'];
        
        //如果此人是属于经纪公司的，此经纪公司是认证过的，则旗下所有的人都是认证过的
        if(!empty($insid)) {
            $userCert = $this->cert_model->get_by_id($insid);
            $ins_user = $this->User_model->get_info_by_id($insid);
        } else {
            //认证
            $userCert = $this->cert_model->get_by_id($userid);
        }

		$oBody = array();
		if($oUser['usertype'] == 1)
		{
			$this->load->model('Userbody_model');
			$oBody = $this->Userbody_model->get_by_id($userid);
		}

		//用户类型、分类
		$userRole = '';
		if ($oUser['usertype'] == 1)
		{
			$userRole = $oUser['role']?$oUser['role']:'模特';
		}
		elseif ($oUser['usertype'] == 2)
		{
			$oSysType = _get_config('type');
			$oSysType = $oSysType[2];
			$oSysStr = '';
			foreach ($oSysType as $key => $v)
            {
            	if(strpos(','.$oUser['type'].',',','.$key.',')>-1)
                {
                	if ($oSysStr)
                    {
                    	$oSysStr .= ','.$v;
                    }
                    else
                    {
                    	$oSysStr = $v;
                    }
                }
            }
            $userRole = $oSysStr?$oSysStr:'机构';
		}
		elseif ($oUser['usertype'] == 3)
		{
			$userRole = '公司';
		}
		elseif ($oUser['usertype'] == 4)
		{
			$userRole = $oUser['role']?$oUser['role']:'摄影师';
		}
		elseif ($oUser['usertype'] == 5)
		{
			$userRole = $oUser['role']?$oUser['role']:'化妆师';
		}
		elseif ($oUser['usertype'] == 6)
		{
			if ($oUser['role'])
			{
				$userRole = $oUser['role'];
			}
			else 
			{
				if ($oUser['type'])
				{
					$oSysType = _get_config('type');
					$oSysType = $oSysType[6];
					$userRole = $oSysType[$oUser['type']];
				}
				else
				{
					$userRole = '艺人';
				}
			}
		}

		//精选作品
		$oGood = $this->user_service->get_user_goodwork($userid);

		$result = array(
			'oUser' => $oUser,
			'oGood' => $oGood,
			'oBody' => $oBody,
            'oCert' => $userCert,
			'userRole' => $userRole
			);
		if (isset($ins_user))
		{
			$result['ins_user'] = $ins_user;
		}

		$view = 'i/index';
		if($oUser['usertype']==2)
			$view = 'i/index_ins';
		else if ($oUser['usertype']==3)
			$view = "i/index_com";
		else if(in_array($oUser['usertype'],array(4,5,6)))
			$view = 'i/index_photo';
		$this->load->view($view,$result);
	}

	//企业主页-通告
	public function activity($id)
	{
		$this->load->service('user_service');
		$oUser = $this->user_service->get_user_homeinfo($id, $this->loginID);
		
		$page = _get_page();
		$pagesize = 10;
		$arrParam = array();
		$arrWhere = array(
			'userid' => $id,
			'status' => 1
		); // 条件
		
		$this->load->model('Activity_model');
		$list = $this->Activity_model->fetch_page($page, $pagesize, $arrWhere);
		
		// 处理展示的字段
		if ( ! empty($list['rows']) )
		{
			foreach ($list['rows'] as $key => $row)
			{
				$list['rows'][$key]['w_time'] = is_numeric($row['begtime']) ? (date('Y-m-d', (int) $row['begtime'])) : $row['begtime'];
				$list['rows'][$key]['endtime'] = date('Y-m-d', $row['inendtime']);
				$list['rows'][$key]['title'] = mb_substr($row['title'], 0, 24);
			}
		}
		
		// 分页
		$pagecfg = array();
		$pagecfg['base_url'] = _create_url('m/activity', $arrParam);
		$pagecfg['total_rows'] = $list['count'];
		$pagecfg['cur_page'] = $page;
		$pagecfg['per_page'] = $pagesize;
		$this->pagination->initialize($pagecfg);
		$list['pages'] = $this->pagination->create_links();
		
		//用户类型、分类
		$userRole = '';
		if ($oUser['usertype'] == 1)
		{
			$userRole = $oUser['role']?$oUser['role']:'模特';
		}
		elseif ($oUser['usertype'] == 2)
		{
			$oSysType = _get_config('type');
			$oSysType = $oSysType[2];
			$oSysStr = '';
			foreach ($oSysType as $key => $v)
			{
				if(strpos(','.$oUser['type'].',',','.$key.',')>-1)
				{
					if ($oSysStr)
					{
						$oSysStr .= ','.$v;
					}
					else
					{
						$oSysStr = $v;
					}
				}
			}
			$userRole = $oSysStr?$oSysStr:'机构';
		}
		elseif ($oUser['usertype'] == 3)
		{
			$userRole = '公司';
		}
		elseif ($oUser['usertype'] == 4)
		{
			$userRole = $oUser['role']?$oUser['role']:'摄影师';
		}
		elseif ($oUser['usertype'] == 5)
		{
			$userRole = $oUser['role']?$oUser['role']:'化妆师';
		}
		elseif ($oUser['usertype'] == 6)
		{
			if ($oUser['role'])
			{
				$userRole = $oUser['role'];
			}
			else
			{
				if ($oUser['type'])
				{
					$oSysType = _get_config('type');
					$oSysType = $oSysType[6];
					$userRole = $oSysType[$oUser['type']];
				}
				else
				{
					$userRole = '艺人';
				}
			}
		}
		
		$result = array(
			'oUser' => $oUser,
			'list' => $list,
			'oSysAct' => _get_config('activity'),
			'userRole' => $userRole
		);
		
		$this->load->view('i/index_activity', $result);
	}
}